{
 "metadata": {
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "D:\\Anaconda\\run\\lib\\site-packages\\ipykernel\\ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n  and should_run_async(code)\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "rank_1_tensor:\n Tensor(shape=[3], dtype=int32, place=CPUPlace, stop_gradient=True,\n       [1, 2, 3])\nrank_2_tensor:\n Tensor(shape=[2, 3], dtype=int32, place=CPUPlace, stop_gradient=True,\n       [[1, 2, 3],\n        [4, 5, 6]])\nrank_1_tensor_comp:\n Tensor(shape=[3], dtype=complex64, place=CPUPlace, stop_gradient=True,\n       [(1+0j), (2+0j), (3+4j)])\n"
     ]
    }
   ],
   "source": [
    "# 使用Python列表或Numpy数组创建Tensor\n",
    "rank_1_tensor = paddle.to_tensor([1, 2, 3]) # 创建一维tensor\n",
    "print('rank_1_tensor:\\n', rank_1_tensor)\n",
    "rank_2_tensor = paddle.to_tensor( # 创建二维tensor\n",
    "    np.array([[1, 2, 3], [4, 5, 6]]))\n",
    "print('rank_2_tensor:\\n', rank_2_tensor)\n",
    "# 和Numpy类似，可使用dtype指定元素类型\n",
    "# 支持bool, float16, float32, float64, unint8, int16, int64\n",
    "# \"complex64\"和\"complex128\"\n",
    "rank_1_tensor_comp = paddle.to_tensor([1, 2, 3+4j],\n",
    "    dtype=\"complex64\")\n",
    "print('rank_1_tensor_comp:\\n', rank_1_tensor_comp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "scalar_array:\n 3.14\nno_scalar_tensor:\n Tensor(shape=[1], dtype=float32, place=CPUPlace, stop_gradient=True,\n       [3.14000010])\n"
     ]
    }
   ],
   "source": [
    "# Tensor不同于ndarray，它没有scalar\n",
    "scalar_array = np.array(3.14)\n",
    "print('scalar_array:\\n', scalar_array)\n",
    "no_scalar_tensor = paddle.to_tensor(3.14)\n",
    "print('no_scalar_tensor:\\n', no_scalar_tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=float32, place=CPUPlace, stop_gradient=True,\n",
       "       [[ 0.14349811, -0.15975733, -1.09784520, -0.36736155],\n",
       "        [ 0.46401772,  0.89276695,  0.30651417,  0.72214061],\n",
       "        [ 2.15094566,  0.04375226, -0.18576820,  0.25322637]])"
      ]
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "source": [
    "# 创建指定形状Tensor的快捷方法\n",
    "m, n, start, end, step, num = 3, 4, 0, 10, 2, 8\n",
    "paddle.zeros([m, n])             # 创建数据全为0，shape为[m, n]的Tensor\n",
    "paddle.ones([m, n])              # 创建数据全为1，shape为[m, n]的Tensor\n",
    "paddle.full([m, n], 10)          # 创建数据全为10，shape为[m, n]的Tensor\n",
    "paddle.arange(start, end, step)  # 创建从start到end，步长为step的Tensor\n",
    "paddle.linspace(start, end, num) # 创建从start到end，元素个数固定为num的Tensor\n",
    "paddle.rand([m, n])    # 创建元素符合[0,1]均匀分布的Tensor\n",
    "paddle.randint(0, 10, [m, n]) # 创建元素符合离散均匀分布的Tensor\n",
    "paddle.randn([m, n])    # 创建元素符合标准正态分布的Tensor\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "shape:  [2, 3]\nndim:  2\nsize:  6\ndtype:  VarType.INT32\nplace:  CPUPlace\nname:  generated_tensor_81\n"
     ]
    }
   ],
   "source": [
    "# Tensor的常用属性\n",
    "print('shape: ', rank_2_tensor.shape)\n",
    "print('ndim: ', rank_2_tensor.ndim)\n",
    "print('size: ', rank_2_tensor.size)\n",
    "print('dtype: ', rank_2_tensor.dtype)\n",
    "print('place: ', rank_2_tensor.place)\n",
    "print('name: ', rank_2_tensor.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "a.shape:  [3, 4]\nb.shape:  [2, 6]\n"
     ]
    }
   ],
   "source": [
    "# 改变Tensor的形状\n",
    "a = paddle.ones([3, 4])\n",
    "print('a.shape: ', a.shape) # [3, 4]\n",
    "# Tensor一旦创建不可更改，因此reshape操作会创建新的Tensor\n",
    "b = paddle.reshape(a, [2, 6])\n",
    "print('b.shape: ', b.shape) # [2, 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "c:  [0 1 2 3 4 5 6 7 8]\n",
      "第一个元素： [0]\n",
      "末尾四个元素： [5 6 7 8]\n",
      "逆序排列： [8 7 6 5 4 3 2 1 0]\n"
     ]
    }
   ],
   "source": [
    "# 通过索引或切片访问Tensor\n",
    "c = paddle.to_tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])\n",
    "print('c: ', c.numpy()) # 使用numpy方法将Tensor转为Numpy数组\n",
    "print('第一个元素：', c[0].numpy())\n",
    "print('末尾四个元素：', c[-4:].numpy())\n",
    "print('逆序排列：', c[::-1].numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Tensor(shape=[1], dtype=bool, place=CPUPlace, stop_gradient=True,\n",
       "       [False])"
      ]
     },
     "metadata": {},
     "execution_count": 49
    }
   ],
   "source": [
    "# Tensor支持的数学运算\n",
    "x = paddle.to_tensor([1., 2., 3.])\n",
    "y = paddle.to_tensor([4., 5., 6.])\n",
    "x.abs()                       #逐元素取绝对值\n",
    "x.ceil()                      #逐元素向上取整\n",
    "x.floor()                     #逐元素向下取整\n",
    "x.round()                     #逐元素四舍五入\n",
    "x.exp()                       #逐元素计算自然常数为底的指数\n",
    "x.log()                       #逐元素计算x的自然对数\n",
    "x.reciprocal()                #逐元素求倒数\n",
    "x.square()                    #逐元素计算平方\n",
    "x.sqrt()                      #逐元素计算平方根\n",
    "x.sin()                       #逐元素计算正弦\n",
    "x.cos()                       #逐元素计算余弦\n",
    "x.add(y)                      #逐元素相加\n",
    "x.subtract(y)                 #逐元素相减\n",
    "x.multiply(y)                 #逐元素相乘\n",
    "x.divide(y)                   #逐元素相除\n",
    "x.mod(y)                      #逐元素相除并取余\n",
    "x.pow(y)                      #逐元素幂运算\n",
    "x.max()                       #指定维度上元素最大值，默认为全部维度\n",
    "x.min()                       #指定维度上元素最小值，默认为全部维度\n",
    "x.prod()                      #指定维度上元素累乘，默认为全部维度\n",
    "x.sum()                       #指定维度上元素的和，默认为全部维度\n",
    "x < y                         #逻辑比较 小于\n",
    "x.allclose(y, rtol=1e-05,     #逻辑比较 全部满足近似条件\n",
    "    atol=1e-08, equal_nan=True, name=\"equal_nan\")  "
   ]
  }
 ]
}